package com.own.component.message.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import com.own.component.autoconfig.AutoConfigInstance;
import com.own.component.common.util.encrypt.EncryptUtil;
import com.own.component.message.model.BaseMessageModel;
import com.own.component.message.service.BaseSmsService;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * MobileSmsServiceImpl
 *
 * @author chenxueli
 * @date 2022-08-15 11:23:00
 */
@Slf4j
public class MobileSmsServiceImpl implements BaseSmsService {

    /**
     * 发送信息
     *
     * @param item 发送的模型信息
     */
    @Override
    public void send(BaseMessageModel<String> item) {
        var phone = item.getReceiver();
        var properties = AutoConfigInstance.INSTANCE.messageProperties.getMobile();
        var sign = properties.getEcName() + properties.getApId() + properties.getSecretKey() + phone + item.getContent() + properties.getSign() + properties.getAddSerial();
        var mac = EncryptUtil.MD5.encrypt(sign);
        var params = new HashMap<String, String>(16);
        params.put("ecName", properties.getEcName());
        params.put("apId", properties.getApId());
        params.put("mobiles", phone);
        params.put("content", item.getContent());
        params.put("sign", properties.getSign());
        params.put("addSerial", properties.getAddSerial());
        params.put("mac", mac);
        var response = HttpRequest.post(properties.getHost())
                .contentType("application/json")
                .body(EncryptUtil.BASE64.encrypt(JSONObject.toJSONString(params)))
                .execute();
        var body = response.body();
        log.debug("body:{}", body);
    }

}
